using System;
using System.Collections.Generic;
using System.Text;
using MaxFramework;

namespace DuplicateFileFinder
{
    //if (a.Equals(b)) {} 
    //if (object.Equals(a, b)) {} 
    //if (object.ReferenceEquals(a, b) {} 
    //if (a == b) {} 
    //if (a is b) {} 

    public class ResultFile
    {
        private System.IO.FileInfo mInfo = null;

        public ResultFile(string pFileName)
        {
            Compared = false;
            FileName = pFileName;
            Name = Info.Name;
            Extension = Info.Extension;
            BinaryMatchList = new List<ResultFile>();
        }

        public ResultFile()
        {

        }

        public string SizeLabel
        {
            get
            {
                long GIGA_SIZE = 1073741824;
                long MEGA_SIZE = 1048576;
                long KILO_SIZE = 1024;
                if (Info.Length == 1) { return "1 Byte"; }
                if (Info.Length < KILO_SIZE) { return Info.Length.ToString("#,###.## Bytes"); }
                if (Info.Length < MEGA_SIZE) { return (Info.Length / KILO_SIZE).ToString("#,###.## KB"); }
                if (Info.Length < GIGA_SIZE) { return (Info.Length / MEGA_SIZE).ToString("#,###.## MB"); }
                else { return (Info.Length / GIGA_SIZE).ToString("#,###.## GB"); }
            }
        }

        public System.IO.FileInfo Info
        {
            get
            {
                if (mInfo == null)
                {
                    mInfo = new System.IO.FileInfo(FileName);
                }
                return mInfo;
            }
        }

        public string FileName
        {
            get;
            set;
        }

        public bool Compared
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Extension
        {
            get;
            set;
        }

        public List<ResultFile> BinaryMatchList
        {
            get;
            set;
        }

        public long Size
        {
            get
            {
                return Info.Length;
            }
        }

        public void MakeWritable()
        {
            Info.MakeWritable();
        }


        //public byte[] Hash
        //{
        //    get;
        //    set;
        //}

        //public void InitHash()
        //{
        //    if (Extension.Equals(".jpg", StringComparison.CurrentCultureIgnoreCase))
        //    {
        //        using (var bmp = new System.Drawing.Bitmap(FileName))
        //        {
        //            Hash = StaticObjects.SHA256Managed.ComputeHash((byte[])StaticObjects.ImageConverter.ConvertTo(bmp, typeof(byte[])));
        //        }
        //    }
        //}

        public bool Compare(ResultFile pResultFile)
        {
            bool tResult = false;

            if (Extension == pResultFile.Extension)
            {
                MakeWritable();
                pResultFile.MakeWritable();

                if (Extension.Equals(".jpg", StringComparison.CurrentCultureIgnoreCase))
                {

                    using (var a = new ImageMagickNET.Image(FileName))
                    {
                        using (var b = new ImageMagickNET.Image(pResultFile.FileName))
                        {
                            tResult = a.Compare(b);
                        }
                    }

                    //if (Hash == null) InitHash();
                    //if (pResultFile.Hash == null) pResultFile.InitHash();

                    //for (int i = 0; i < Hash.Length && i < pResultFile.Hash.Length; i++)
                    //{
                    //    if (!(Hash[i] == pResultFile.Hash[i]))
                    //    {
                    //        tResult = false;
                    //        break;
                    //    }
                    //}

                    //using (var bmpA = new System.Drawing.Bitmap(FileName))
                    //{
                    //    using (var bmpB = new System.Drawing.Bitmap(pResultFile.FileName))
                    //    {

                    //        if (bmpA.Width == bmpB.Width && bmpA.Height == bmpB.Height)
                    //        {
                    //            bool tEqual = true;
                    //            for (int i = 0; i < bmpA.Width; i++)
                    //            {
                    //                for (int j = 0; j < bmpB.Height; j++)
                    //                {

                    //                    if (bmpA.GetPixel(i, j) != bmpB.GetPixel(i, j))
                    //                    {
                    //                        tEqual = false;
                    //                        break;
                    //                    }
                    //                }
                    //            }
                    //        }
                    //    }


                    //}
                }
                else
                {

                    using (var fsA = new System.IO.FileStream(FileName, System.IO.FileMode.Open))
                    {
                        using (var brA = new System.IO.BinaryReader(fsA))
                        {
                            using (var fsB = new System.IO.FileStream(pResultFile.FileName, System.IO.FileMode.Open))
                            {
                                using (var brB = new System.IO.BinaryReader(fsB))
                                {
                                    if (brA.BaseStream.Length == brB.BaseStream.Length)
                                    {
                                        bool tValidA = false;
                                        bool tValidB = false;
                                        bool tEqual = true;

                                        while (tValidA && tValidB)
                                        {


                                            byte[] tBufferA = brA.NextRead(out tValidA);
                                            byte[] tBufferB = brB.NextRead(out tValidB);

                                            if (tValidA && tValidB)
                                            {
                                                if (tBufferA.Length == tBufferB.Length)
                                                {
                                                    for (int i = 0; i < tBufferA.Length; i++)
                                                    {
                                                        if (tBufferA[i] != tBufferB[i])
                                                        {
                                                            tEqual = false;
                                                            break;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    tEqual = false;
                                                    break;
                                                }
                                            }
                                        }

                                        if (tEqual && (tValidA == tValidB))
                                        {
                                            tResult = true;
                                        }
                                    }
                                    brB.Close();

                                }
                                fsB.Close();
                            }
                            brA.Close();
                        }
                        fsA.Close();
                    }
                }
            }


            return tResult;
        }

    }
}
